import os
import sys
import logging
import pygame

try:
    __file__
except NameError as e:
    from whereis import mymain
    rootdir = mymain()
else:
    rootdir = os.path.dirname(os.path.dirname(__file__))

sys.path.insert(0, rootdir)
from lib.bashlog import getlogger
from lib.tracemore import get_exc_plus
from lib.minsix import callable
from lib.config import Config
from lib.rec import Button
from lib.checkbox import CheckBox
from lib.sprite import Sprite
from lib.sprite import Group
from lib import tool
sys.path.pop(0)


logger = logging.getLogger('wordson.modepage')

cfg = Config()

# todo: don't copy myself

class ModePage(Group):
    this_page = cfg.page_mode
    screenx = cfg.screen[0] / 2

    def __init__(self, title, select, empty, prevpage, **evtattrs):
        super(BasePage, self).__init__()


        titlefont = cfg.new_font(50)
        image = tool.render(titlefont, cfg.mode_title)
        title = Sprite()
        rect = image.get_rect()
        rect.top = 70
        rect.centerx = self.screenx
        title.image = image
        title.rect = rect


        img = tool.render(cfg.font, getattr(cfg, 'return'))
        rect = img.get_rect()
        rect.width += 60
        rect.height += 30
        rect.top = 480

        rtnbtn = Button(img, rect, events.SWITCHPAGE, to=cfg.page_cate, by=self.this_page)
        rtnbtn.rect.centerx = cfg.screen[0] / 2

        self.add(title)
        self.add(rtnbtn)


if __name__ == '__main__':
    getlogger(logger, logging.DEBUG)

    pygame.init()
    pygame.key.set_repeat(cfg.key_repeat_delay, cfg.key_repeat_interval)
    pygame.display.set_caption(cfg.title)

    screen = pygame.display.set_mode(cfg.screen)

    run = True
    clock = pygame.time.Clock()
    page = MainPage()
    # btn = Button("test")

    while run:
        for event in pygame.event.get():
            if page.handle(event):
                continue
            if event.type == pygame.QUIT:
                run = False

        inter = clock.tick(cfg.tick)
        page.update(inter)
        screen.fill(color.white)
        page.draw(screen)
        pygame.display.flip()

    pygame.quit()
